package com.glink.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.silentresult.*;
import com.glink.manage.service.SilentResultService;
import com.glink.manage.vo.silentresult.SilentResultPageVO;
import com.glink.manage.vo.silentresult.SilentResultWarnPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author  qiushaoshan
 * @since  2025-01-20
 */
@RestController
@RequestMapping("/API/Manage/silentResult")
@Api(tags = "违章和告警")
@Slf4j
public class SilentResultController extends AbstractBaseController {

    @Autowired
    private SilentResultService silentResultService;

    @ApiOperation(value = "01.分页查询待处理违章")
    @PostMapping(value = "/pageByPendingViolationHandling", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    public ResponseResult<IPage<SilentResultPageVO>> pageByPendingViolationHandling(@Validated @RequestBody SilentResultPageDTO dto) throws GeneralSimpleException {
        log.info("01.分页查询待处理违章：{}", LogUtil.encode(dto));
        IPage<SilentResultPageVO> pageVOIPage = silentResultService.pageByPendingViolationHandling(dto);
        log.info("01.分页查询待处理违章：success");
        return this.response(pageVOIPage);
    }

    @ApiOperation(value = "02.忽略违章(支持批量处理)")
    @PostMapping(value = "/ignoreViolations", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> ignoreViolations(@Validated @RequestBody   List<String> objIds) throws GeneralSimpleException {
        log.info("02.忽略违章：{}", LogUtil.encode(objIds));
        Boolean status = silentResultService.ignoreViolations(objIds);

        log.info("02.忽略违章：success");
        return this.response(status);
    }

    @ApiOperation(value = "03.处理违章")
    @PostMapping(value = "/handleViolations", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> handleViolations(@Validated @RequestBody SilentResultHandleDTO dto) throws GeneralSimpleException {
        log.info("03.处理违章：{}", LogUtil.encode(dto));
        Boolean status = silentResultService.handleViolations(dto);
        log.info("03.处理违章：success");
        return this.response(status);
    }

    @ApiOperation(value = "04.分页查询告警信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/pageByWarn", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<SilentResultWarnPageVO>> pageByWarn(@Validated @RequestBody SilentResultWarnPageDTO dto) throws GeneralSimpleException {
        log.info("04.分页查询告警信息：{}", LogUtil.encode(dto));
        IPage<SilentResultWarnPageVO> warnPageVOIPage = silentResultService.pageByWarn(dto);
        log.info("04.分页查询告警信息：success");
        return this.response(warnPageVOIPage);
    }

    @ApiOperation(value = "05.告警信息已读")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/readWarn", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> readWarn(@Validated @RequestBody SilentResultWarnReadDTO dto) throws GeneralSimpleException {
        log.info("05.告警信息已读：{}", LogUtil.encode(dto));
        boolean status = silentResultService.readWarn(dto);
        log.info("05.告警信息已读：success");
        return this.response(status);
    }


    @ApiOperation(value = "06.下载FTP文件(type:1-原图。2-结果图)")
    @GetMapping(value = "/downloadPic/{type}/{resultId}")
    public void downloadPic(@Validated @PathVariable("type") String type, @Validated @PathVariable("resultId") String resultId, HttpServletResponse response) throws Exception {
        log.info("06.下载FTP文件：type:{}, resultId:{}", LogUtil.encode(type), LogUtil.encode(resultId));
        silentResultService.downloadPicByFtp(type, resultId, response);
    }
}
